package com.virtualtrainer_01.view;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.Spinner;
import android.widget.Toast;

import com.virtualtrainer_01.R;
import com.virtualtrainer_01.dao.ClientDB;
import com.virtualtrainer_01.model.Client;
import com.virtualtrainer_01.model.TrainingDay;
import com.virtualtrainer_01.service.GenerateClientTraining;
import com.virtualtrainer_01.util.NameList;
import com.virtualtrainer_01.viewListeners.GenerateListAdapter;

public class GenerateTraining extends Activity {
	private ExpandableListView exp;
	private Spinner spinner;
	private String[] days;
	private ClientDB clientDB;
	private Client client;
	NameList<TrainingDay> antrenamente;

	public void addListenerOnSpinnerItemSelection() {
		spinner = (Spinner) findViewById(R.id.spinnerGenerate);
		getActionBar().setDisplayHomeAsUpEnabled(true);

		spinner.setOnItemSelectedListener(new OnItemSelectedListener() {

			public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {

				exp = (ExpandableListView) findViewById(R.id.expandableListGenerate);
				exp.setAdapter(new GenerateListAdapter(GenerateTraining.this, spinner.getSelectedItemPosition(), antrenamente));

				// Toast.makeText(parent.getContext(),
				// spinner.getSelectedItemPosition()+"",Toast.LENGTH_SHORT).show();
				exp.setOnChildClickListener(new OnChildClickListener() {

					@Override
					public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
						try {
							String exName = ((GenerateListAdapter) exp.getExpandableListAdapter()).getChildList()[groupPosition][childPosition];
							Intent intent = new Intent(GenerateTraining.this, Description.class);
							intent.putExtra("cheia", exName);
							startActivityForResult(intent, 1);
						} catch (Exception ex) {
							ex.printStackTrace();
							Toast.makeText(GenerateTraining.this, "Error", Toast.LENGTH_LONG).show();
						}
						return false;
					}
				});
				/*
				 * Toast.makeText( parent.getContext(),
				 * "OnItemSelectedListener : " +
				 * parent.getItemAtPosition(pos).toString(),
				 * Toast.LENGTH_SHORT).show();
				 */
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub
			}
		});
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.generatetraining);
		getActionBar().setDisplayHomeAsUpEnabled(true);
		spinner = (Spinner) findViewById(R.id.spinnerGenerate);

		clientDB = new ClientDB(this);
		clientDB.open();
		client = clientDB.getClient();
		clientDB.close();

		GenerateClientTraining gen = new GenerateClientTraining(this);
		gen.Generate();
		antrenamente = gen.getTrainingDayList();
		days = new String[client.getDisponibility()];
		for (int i = 0; i < client.getDisponibility(); i++) {
			days[i] = "Day " + (i + 1);
		}

		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, days);
		spinner.setAdapter(adapter);

		addListenerOnSpinnerItemSelection();
	}

}
